package jhsys.mc.broadcast;

import jhsys.mc.basepage.MCBasepage;
import jhsys.mc.basepage.MCBasepage.FQAlarmHandler;
import jhsys.mc.basepage.MCBasepage.FQHandler;
import jhsys.mc.basepage.MCBasepage.PAGEHandler;
import jhsys.mc.basepage.MCBasepage.SmartHomeHandler;
import jhsys.mc.basepage.MCBasepage.WIFIHandler;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.util.Log;

/**
 * @author pengxj
 * @email pengxj@jhsys.cn
 * @created 2011-7-13
 */
public class DefaultBroadcastReceiver extends BroadcastReceiver {

	public static final String TAG = "DefaultBroadcastReceiver";
	
	public void onReceive(Context context, Intent intent) {
		if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
//          Intent bootActivityIntent=new Intent(context,jhsys.mc.UI.MainActivity.class);
//          bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//          context.startActivity(bootActivityIntent);
		}
		int actionID = DefaultIntentFilter.getActionID(intent.getAction());
		Log.d(TAG, "ACTION-:" + intent.getAction() + "\t:" + actionID);
		switch (actionID) {
		case DefaultIntentFilter.ACTION_ID_SMARTHOME_STATE:
			handleSmarthomeState(intent);
			break;
		case DefaultIntentFilter.ACTION_ID_FQDIALOG_PROCESS:
			handleFQDialogProcess(intent);
			break;
		case DefaultIntentFilter.ACTION_ID_WIFISTATE:
			handleWifiState(intent);
			break;
		case DefaultIntentFilter.ACTION_ID_PAGESTATE:
			handlePageState(intent);
			break;
		case DefaultIntentFilter.ACTION_ID_FQSTATE:
			handleFqState(intent);
			break;
		}
	}

	private void handleFQDialogProcess(Intent intent) {
		int type = intent.getIntExtra("type", -1);
		MCBasepage.AlarmType=type;
		MCBasepage currentPage = MCBasepage.getCurrentInstance(); 
		if(currentPage != null) {
			FQAlarmHandler handler = currentPage.new FQAlarmHandler(Looper.getMainLooper());
			handler.sendEmptyMessage(type);
			Log.d(TAG, "type="+type);
		}
	}
	
	private void handleSmarthomeState(Intent intent) {
		int status=intent.getIntExtra("status", -1);
		MCBasepage currentPage = MCBasepage.getCurrentInstance();
		if(currentPage != null) {
			SmartHomeHandler handler= currentPage.new SmartHomeHandler(Looper.getMainLooper());
			handler.sendEmptyMessage(status);
			Log.d(TAG, "status="+status);
		}
	}
	
	private void handleWifiState(Intent intent){
	    int wifistatus = intent.getIntExtra("wifistate", -1);
		MCBasepage currentPage = MCBasepage.getCurrentInstance();
		if(currentPage != null) {
			WIFIHandler handler = currentPage.new WIFIHandler(Looper.getMainLooper());
			handler.sendEmptyMessage(wifistatus);
			Log.d(TAG, "wifistatus="+wifistatus);
		}
	}
	private void handlePageState(Intent intent){
	    int pagestatus = intent.getIntExtra("pagestate", -1);
		MCBasepage currentPage = MCBasepage.getCurrentInstance();
		if(currentPage != null) {
			PAGEHandler handler = currentPage.new PAGEHandler(Looper.getMainLooper());
			handler.sendEmptyMessage(pagestatus);
			Log.d(TAG, "pagestatus="+pagestatus);
		}
	}
	private void handleFqState(Intent intent){
	    int fqstatus = intent.getIntExtra("fqstate", -1);
		MCBasepage currentPage = MCBasepage.getCurrentInstance();
		if(currentPage != null) {
			FQHandler handler = currentPage.new FQHandler(Looper.getMainLooper());
			handler.sendEmptyMessage(fqstatus);
			Log.d("AF", "fqstatus="+fqstatus);
		}
	}
	
}
